package fr.univ.context.process;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import fr.univ.context.utilities.Constants;
import fr.univ.context.utilities.CustomAdapter;
import fr.univ.context.utilities.Preferences;

import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

public class MainActivity extends ListActivity {
	
	private Preferences preferences;
	private ListView listView;
	
	@Override
	public void onCreate(Bundle bundle){
		
		super.onCreate(bundle);
		setContentView(R.layout.activity_main);
		
		List<String> list = new ArrayList<String>();
        Collections.addAll(list, Constants.TICKER_SYMBOLS);

        setListAdapter( new CustomAdapter(this, list) );
		
		preferences = new Preferences(this);
		
		listView = getListView();
		listView.setDividerHeight(2);
		
		String filePath = new String(Constants.MAIN_PACKAGE.substring(Constants.MAIN_PACKAGE.lastIndexOf('.') + 1));
		
		Log.i("Exec", "NAME: " + filePath);
	}
	
	@Override
	protected void onListItemClick(ListView l, View v, int position, long id){
		
		super.onListItemClick(l, v, position, id);
		
		switch(position){
		
			case 1:
				
				AlertDialog.Builder capInterval = new AlertDialog.Builder(this);
				capInterval.setTitle("Capture interval");
		        capInterval.setIcon(R.drawable.freq);
				capInterval.setSingleChoiceItems( optionsArrayString(Constants.TIME_CAPTURE_FREQUENCY) , 0, null);
		        capInterval.setPositiveButton("OK", new DialogInterface.OnClickListener() {
		            
		        	public void onClick(DialogInterface dialog, int whichButton) {
		                int i = ((AlertDialog)dialog).getListView().getCheckedItemPosition();
		                
		                TextView tx = (TextView) findViewById(R.id.subtitle_set_interval);
		                tx.setText(Constants.TIME_CAPTURE_FREQUENCY[i] + " minutes");
		                
		                Log.i("Exec", "Value: " + Constants.TIME_CAPTURE_FREQUENCY[i]);
		                
		                preferences.save(Constants.KEY_TIME_SLEEP, Constants.TIME_CAPTURE_FREQUENCY[i]);
		                dialog.dismiss();
		            }
		        });
		        
		        capInterval.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();						
					}
				}).show();
				
				break;
			
			case 2:
				
				AlertDialog.Builder deleteDatabase = new AlertDialog.Builder(this);
				deleteDatabase.setTitle("Delete");
				deleteDatabase.setIcon(R.drawable.delete);
				deleteDatabase.setMessage("Database will be deleted.");
				
				deleteDatabase.setPositiveButton("OK", new DialogInterface.OnClickListener() {
					
					@Override
					public void onClick(DialogInterface dialog, int which) {
						
						boolean isDelete = MainActivity.this.deleteDatabase(Constants.DATABASE_NAME);
						Log.i("Exec", "Apagou? " + isDelete );
					}
				});
				
				deleteDatabase.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();						
					}
				}).show();
		}
	}

	private String[] optionsArrayString(int[] intArray){
		
		String[] stringArray = new String[intArray.length];
		
		for (int i = 0; i < stringArray.length; i++)
			stringArray[i] = intArray[i] + " min";
		
		return stringArray;
	}
}
